package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.encrypts.des.DESEncode;
import com.sxbbc.common.core.entity.jz.SysRoles;
import com.sxbbc.common.core.entity.jz.SysUser;
import com.sxbbc.common.core.mapper.jz.SysUserMapper;
import com.sxbbc.common.core.service.ISysRolesService;
import com.sxbbc.common.core.service.ISysUserService;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 * 平台用户表 服务实现类
 * </p>
 *
 * @author arno
 * @since 2019-04-26
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private ISysRolesService sysRolesService;

    @Override
    public SysUser selectByLogin(String login) {
        return super.getOne(new QueryWrapper<SysUser>().eq("login", login));
    }

    @Override
    public BasePage<SysUser> queryList(BasePage<SysUser> page, SysUser sysUser) {
        QueryWrapper<SysUser> wrapper = queryWrapper(sysUser);
        if (page.isPaging()) {
            super.page(page, wrapper);
        } else {
            List<SysUser> list = super.list(wrapper);
            page.setRecords(list);
        }
        for (SysUser u : page.getRecords()) {
            SysRoles roles = sysRolesService.getById(u.getRoleid());
            if(roles != null){
                u.setRoleName(roles.getName());
            }
        }
        return page;
    }

    @Override
    public RestResponse updatePassword(Integer userId, String oldPwd, String newPwdOne, String newPwdTwo) {
        SysUser user = super.getById(userId);
        if (userId == null || user == null) {
            return GetRest.getFail("该用户不存在或者登录异常");
        }
        if (StringUtils.isBlank(oldPwd)) {
            return GetRest.getFail("请输入原密码");
        }
        if (StringUtils.isBlank(newPwdOne) || StringUtils.isBlank(newPwdTwo)) {
            return GetRest.getFail("请输入新密码");
        }
        if (newPwdOne.length() < 6 || newPwdTwo.length() < 6) {
            return GetRest.getFail("密码长度不能少于6位");
        }
        // 加密类实例
        DESEncode des = DESEncode.getInstance();
        // 原密码加密
        String oldPassword = des.encrypt(oldPwd);
        // 两次新密码加密
        String newPasswordOne = des.encrypt(newPwdOne);
        String newPasswordTwo = des.encrypt(newPwdTwo);
        if (!StringUtils.equals(oldPassword,user.getPassword())) {
            return GetRest.getFail("原密码不正确");
        }
        if (!StringUtils.equals(newPasswordOne,newPasswordTwo)) {
            return GetRest.getFail("两次输入的新密码不一致");
        }
        user.setPassword(newPasswordOne);
        boolean b = super.saveOrUpdate(user);
        if (!b) {
            throw new ThrowJsonException("密码修改失败");
        }
        return GetRest.getSuccess("修改成功");
    }

    @Override
    public RestResponse handleUserStatus(Integer id, Integer status) {
        SysUser user = super.getById(id);
        if (user == null) {
            return GetRest.getFail("操作员信息异常，请刷新重试");
        }
        user.setStatus(status);
        boolean save = super.updateById(user);
        if (!save) {
            throw new ThrowJsonException("保存操作员状态异常");
        }
        return GetRest.getSuccess("操作成功");
    }

    /**
     * sysUser公用查询
     * @param sysUser 查询条件
     * @author leehao
     * @return com.baomidou.mybatisplus.mapper.EntityWrapper<com.sxbbc.common.core.entity.jz.SysUser>
     */
    private QueryWrapper<SysUser> queryWrapper(SysUser sysUser) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(sysUser.getLogin())) {
            wrapper.eq("login",sysUser.getLogin());
        }
        if (StringUtils.isNotBlank(sysUser.getName())) {
            wrapper.eq("name",sysUser.getName());
        }
        return wrapper;
    }
}
